package com.educatioin.manage.wechat.controller;

import com.educatioin.manage.common.beans.Response;
import com.educatioin.manage.utils.ServerUtil;
import com.educatioin.manage.web.productorder.po.ProductOrderPo;
import com.educatioin.manage.web.productorder.repository.ProductOrderRepository;
import com.educatioin.manage.web.studytours.po.StudyAbroadProductPo;
import com.educatioin.manage.web.studytours.po.StudyToursProductPo;
import com.educatioin.manage.web.studytours.repository.StudyAbroadProductRepository;
import com.educatioin.manage.web.studytours.repository.StudyToursProductRepository;
import com.educatioin.manage.wechat.request.CreateOrderReq;
import com.educatioin.manage.wechat.response.CreateOrderRsp;
import com.educatioin.manage.wechat.response.GetOrderByCodeRsp;
import com.educatioin.manage.wechat.response.QueryOrdersRsp;
import com.educatioin.manage.wechat.service.WeChatOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhangchen on 2018/4/20.
 */
@RestController
@RequestMapping("/wx/api/order")
public class WeChatOrderController {
    @Autowired
    private WeChatOrderService weChatOrderService;
    @Autowired
    private ProductOrderRepository productOrderRepository;
    @Autowired
    private StudyToursProductRepository studyToursProductRepository;
    @Autowired
    private StudyAbroadProductRepository studyAbroadProductRepository;

    /**
     * 创建订单接口
     * @param createOrderReq
     * @return
     */
    @RequestMapping("/createOrder")
    @ResponseBody
    public CreateOrderRsp createOrder(@RequestBody CreateOrderReq createOrderReq, HttpServletRequest request) throws IOException {
        CreateOrderRsp createOrderRsp = new CreateOrderRsp();
        //创建订单,第一次购买的客户同步创建账号
        if(StringUtils.isBlank(createOrderReq.getOpenid())){
            createOrderRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            createOrderRsp.setReturnMsg("未获得open_id");
            return createOrderRsp;
        }
        String requestIp = ServerUtil.getIpAddress(request);
        createOrderReq.setRequestIp(requestIp);
        return weChatOrderService.createOrder(createOrderReq);
    }

    /**
     * 获取订单详情接口
     * @param orderCode
     * @return
     */
    @RequestMapping("/getOrderByCode")
    @ResponseBody
    public GetOrderByCodeRsp getOrderByCode(String orderCode) {
        GetOrderByCodeRsp getOrderByCodeRsp = new GetOrderByCodeRsp();

        ProductOrderPo productOrderPo = productOrderRepository.findByOrderCode(orderCode);
        if(null == productOrderPo){
            getOrderByCodeRsp.setReturnCode(Response.RETURN_FAIL_CODE);
            getOrderByCodeRsp.setReturnMsg("订单不存在！");
            return getOrderByCodeRsp;
        }

        //根据productCode获取产品
        Integer productType = productOrderPo.getProductType();
        if(1 == productType) { //留学产品
            StudyAbroadProductPo studyAbroadProductPo = studyAbroadProductRepository.findByCode(productOrderPo.getProductCode());
            if (null == studyAbroadProductPo) {
                getOrderByCodeRsp.setReturnCode(Response.RETURN_FAIL_CODE);
                getOrderByCodeRsp.setReturnMsg("产品不存在！");
                return getOrderByCodeRsp;
            }
            getOrderByCodeRsp.setShowImage(studyAbroadProductPo.getShowImage());

        }else{ //游学产品
            StudyToursProductPo studyToursProductPo = studyToursProductRepository.findByCode(productOrderPo.getProductCode());
            if (null == studyToursProductPo) {
                getOrderByCodeRsp.setReturnCode(Response.RETURN_FAIL_CODE);
                getOrderByCodeRsp.setReturnMsg("产品不存在！");
                return getOrderByCodeRsp;
            }
            getOrderByCodeRsp.setShowImage(studyToursProductPo.getShowImage());

        }
        getOrderByCodeRsp.setReturnCode(Response.RETURN_SUCCESS_CODE);
        getOrderByCodeRsp.setApplicantName(productOrderPo.getApplicantName());
        getOrderByCodeRsp.setGroupName(productOrderPo.getGroupName());
        getOrderByCodeRsp.setPhone(productOrderPo.getPhone());
        getOrderByCodeRsp.setOrderCode(orderCode);
        getOrderByCodeRsp.setProductName(productOrderPo.getProductName());
        getOrderByCodeRsp.setSchoolGradeClazz(productOrderPo.getSchool() + "," + productOrderPo.getGrade() + "," + productOrderPo.getClazz());
        getOrderByCodeRsp.setProductDeposit(productOrderPo.getProductDeposit());

        getOrderByCodeRsp.setOrderStatus(productOrderPo.getOrderStatus());
        getOrderByCodeRsp.setOrderTime(stampToDate(productOrderPo.getCreateTime()));

        return getOrderByCodeRsp;
    }

    /**
     * 获取订单列表
     * @param request
     * @return
     */
    @RequestMapping("/queryOrders")
    @ResponseBody
    public QueryOrdersRsp queryOrders(HttpServletRequest request) {
        return weChatOrderService.queryOrdersByToken(request);
    }

    private String stampToDate(Long timeStamp){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(timeStamp);
        return simpleDateFormat.format(date);
    }
}
